//
//  SRDate.m
//  CarRental
//
//  Created by 孙锐 on 2017/6/13.
//  Copyright © 2017年 孙锐. All rights reserved.
//

#import "SRDate.h"

@implementation SRDate


+(NSString *)fromDateToNSString:(NSDate *)date AndFormatter:(NSString *)format {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSString *dateNSString = [formatter stringFromDate:date];

    return dateNSString;
}

+(NSDate *)getNowTime{
    
    NSDate *date = [NSDate date]; // 获得时间对象
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    
    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    
    return dateNow;
}

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    return timeSp;
}

+(NSDate *)dateFromString:(NSString *)str AndFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSDate *date = [formatter dateFromString:str];
    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    
    return dateNow;
}

+(int)getTwoDaysBetween:(NSString *)str{
    
    //现在的时间
    NSDate * nowDate = [NSDate date];
    NSString *string = [self fromDateToNSString:nowDate AndFormatter:@"YYYY-MM-dd"];
    NSDate *date = [self dateFromString:string AndFormatter:@"YYYY-MM-dd"];
    //要转换的字符串
    NSString * dateString = str;
    NSArray *arr = [dateString componentsSeparatedByString:@" "];
    //字符串转NSDate格式的方法
    NSDate * ValueDate = [self dateFromString:arr[0] AndFormatter:@"YYYY-MM-dd"];
    //计算两个中间差值(秒)
    NSTimeInterval time = [date timeIntervalSinceDate:ValueDate];
    
    //开始时间和结束时间的中间相差的时间
    int days;
    days = ((int)time)/(3600*24);  //一天是24小时*3600秒
    NSString * dateValue = [NSString stringWithFormat:@"%d",days];
    
    NSLog(@"打印出时间的差值--->>%@",dateValue);
    return days;
}

+(int)getTwoDaysBetweenWithDate:(NSDate *)date{
    
    //现在的时间
    NSDate * nowDate = [NSDate date];
    

    //字符串转NSDate格式的方法
    NSDate * ValueDate = date;
    //计算两个中间差值(秒)
    NSTimeInterval time = [nowDate timeIntervalSinceDate:ValueDate];
    
    //开始时间和结束时间的中间相差的时间
    int days;
    days = ((int)time)/(3600*24);  //一天是24小时*3600秒
    NSString * dateValue = [NSString stringWithFormat:@"%d",days];
    
    NSLog(@"打印出时间的差值--->>%@",dateValue);
    return days;
}

+(int)getTwoDaysBetween:(NSDate *)dateBegin andDate:(NSDate *)dateEnd{
    //字符串转NSDate格式的方法
    NSDate * ValueDate = dateBegin;
    //计算两个中间差值(秒)
    NSTimeInterval time = [dateEnd timeIntervalSinceDate:ValueDate];
    
    //开始时间和结束时间的中间相差的时间
    int days;
    days = ((int)time)/(3600*24);  //一天是24小时*3600秒
    NSString * dateValue = [NSString stringWithFormat:@"%d",days];
    
    NSLog(@"打印出时间的差值--->>%@",dateValue);
    return days;
}

+(NSDate *)getDateWithDate:(NSDate *)needDate{
    
    NSDate *date = needDate; // 获得时间对象
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    
    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    
    return dateNow;
}

+(int)getDayWithDate:(NSDate *)date{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:date];
    return (int)[comps weekday];
}

+(NSString *)intToWeekDay:(int)day{
    NSString *string;
    switch (day) {
        case 1:
            string = @"星期日";
            break;
        case 2:
            string = @"星期一";
            break;
        case 3:
            string = @"星期二";
            break;
        case 4:
            string = @"星期三";
            break;
        case 5:
            string = @"星期四";
            break;
        case 6:
            string = @"星期五";
            break;
        case 7:
            string = @"星期六";
            break;
            
        default:
            string = @"星期日";
            break;
    }
    return string;
}





@end
